﻿using System; 
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common.Helper;

namespace Besture.UI.Admin.Control
{
    public partial class RoomUserControl : System.Web.UI.UserControl
    {
        public bool required { get; set; }

        public long _HotelsID;

        /// <summary>
        /// 酒店编号
        /// </summary>
        public long HotelsID
        {
            get { return string.IsNullOrEmpty(ddlHotels.SelectedValue) ? 0 : Convert.ToInt32(ddlHotels.SelectedValue); }
            set { _HotelsID = HotelsID; }
        }

        public string _HotelsName;

        /// <summary>
        /// 酒店名称
        /// </summary>
        public string ProvinceName
        {
            get { return string.IsNullOrEmpty(ddlHotels.SelectedItem.Text) ? "" : ddlHotels.SelectedItem.Text; }
            set { _HotelsName = ProvinceName; }
        }

        public long _RoomID;

        /// <summary>
        /// 房型编号
        /// </summary>
        public long RoomID
        {
            get { return  string.IsNullOrEmpty(ddlRoom.SelectedValue) ? 0 : Convert.ToInt32(ddlRoom.SelectedValue); }
            set { _RoomID = RoomID; }
        }

        public string _RoomName;

        /// <summary>
        /// 城房型名称
        /// </summary>
        public string CityName
        {
            get { return string.IsNullOrEmpty(ddlRoom.SelectedItem.Text) ? "" : ddlRoom.SelectedItem.Text; }
            set { _RoomName = CityName; }
        }
         

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定酒店份数据
                DataTable tableProvince = QueryDac.GetTable("select * from TBHotels where IsDeleted=0 order by SortID");
                ControlHelper.BindDropDownList(ddlHotels, tableProvince, "HotelsName", "ID");
                InitDropDownList(ddlRoom, null); 
                if (required)
                {
                    ddlRoom.CssClass = "cityRequired";
                }

                //初始化
                if (_HotelsID > 0)
                {
                    ddlHotels.SelectedValue = _HotelsID.ToString();
                    //加载房型数据
                    string sSql = string.Format(
                   "SELECT tbp.RoomName,tbp.ID,tbp.SortID  from TBRoom tbp JOIN TBRoomPrice tbrp ON tbp.ID=tbrp.RoomID where tbp.IsDeleted=0 and tbp.HotelsID={0} GROUP BY tbp.RoomName,tbp.ID,tbp.SortID  order by tbp.SortID ",
                   _HotelsID);
                    DataTable tableCity = QueryDac.GetTable(sSql);
                    BindDropDownList(ddlRoom, tableCity, "RoomName", "ID"); 
                }
                if (_RoomID > 0)
                {
                    ddlRoom.SelectedValue = _RoomID.ToString();
                    
                } 
            }

        }


        /// <summary>
        /// 选择酒店
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlHotels_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取选择酒店
            if (string.IsNullOrEmpty(ddlHotels.SelectedValue) || ddlHotels.SelectedValue.Equals("0"))
            {
                InitDropDownList(ddlRoom, null);
                return;
            }
            int proID = Convert.ToInt32(ddlHotels.SelectedValue);
            string sSql = string.Format(
               "SELECT tbp.RoomName,tbp.ID,tbp.SortID  from TBRoom tbp JOIN TBRoomPrice tbrp ON tbp.ID=tbrp.RoomID where tbp.IsDeleted=0 and tbp.HotelsID={0} GROUP BY tbp.RoomName,tbp.ID,tbp.SortID  order by tbp.SortID ",
               proID);
            DataTable tableCity = QueryDac.GetTable(sSql);
           // DataTable tableCity = QueryDac.GetTable("select * from TBRoom where IsDeleted=0 and HotelsID=" + proID + " order by SortID");
            BindDropDownList(ddlRoom, tableCity, "RoomName", "ID");
            //绑定数据后重新注册一下JS事件
            GetRegister();
        }

        /// <summary>
        /// 绑定数据后重新注册一下JS事件
        /// </summary>
        private void GetRegister()
        {
            //绑定数据后重新注册一下JS事件
            ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, GetType(), "GetQuery", "GetQuery();", true); 

        }

        /// <summary>
        /// 选择房型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取选择房型
            if (string.IsNullOrEmpty(ddlRoom.SelectedValue) || ddlRoom.SelectedValue.Equals("0"))
            { 
                return;
            } 
            //绑定数据后重新注册一下JS事件
            GetRegister();
        }

        /// <summary>
        /// 重新加载
        /// </summary>
        public void PostBack()
        {
            //初始化
            if (_HotelsID > 0)
            {
                ddlHotels.SelectedValue = _HotelsID.ToString();
                //加载房型数据
                string sSql = string.Format(
              "SELECT tbp.RoomName,tbp.ID,tbp.SortID  from TBRoom tbp JOIN TBRoomPrice tbrp ON tbp.ID=tbrp.RoomID where tbp.IsDeleted=0 and tbp.HotelsID={0} GROUP BY tbp.RoomName,tbp.ID,tbp.SortID  order by tbp.SortID ",
              _HotelsID);
                DataTable tableCity = QueryDac.GetTable(sSql);
                //DataTable tableCity = QueryDac.GetTable("select * from TBRoom where IsDeleted=0 and HotelsID=" + _HotelsID + " order by SortID");
                BindDropDownList(ddlRoom, tableCity, "RoomName", "ID"); 
            }
            if (_RoomID > 0)
            {
                ddlRoom.SelectedValue = _RoomID.ToString(); 
            } 

        }




        /// <summary>
        /// 绑定下拉列表
        /// </summary>
        /// <param name="sourceTable"></param>
        /// <param name="dataTextField"></param>
        /// <param name="dataValueField"></param>
        private static void BindDropDownList(DropDownList dropDownList, DataTable sourceTable, string dataTextField, string dataValueField)
        {
            const string defaultItemStr = "---全部房型---";
            if (sourceTable != null)
            {
                dropDownList.DataSource = sourceTable;
                dropDownList.DataTextField = dataTextField;
                dropDownList.DataValueField = dataValueField;
                dropDownList.DataBind();
            }
            dropDownList.Items.Insert(0, new ListItem(defaultItemStr, "0"));
            dropDownList.SelectedIndex = 0;
        }

        /// <summary>
        /// 初始化下拉框
        /// </summary>
        /// <param name="dropDownList"></param>
        /// <param name="initText"></param>
        private static void InitDropDownList(DropDownList dropDownList, string initText)
        {
            dropDownList.Items.Clear();
            string defaultItemStr = string.IsNullOrEmpty(initText) ? "---全部房型---" : initText;
            dropDownList.Items.Insert(0, new ListItem(defaultItemStr, ""));
            dropDownList.SelectedIndex = 0;
        }


        /// <summary>
        /// 初始化数据
        /// </summary>
        public  void InitData()
        {
            DataTable tableProvince = QueryDac.GetTable("select * from TBHotels where IsDeleted=0 order by SortID ");
            ControlHelper.BindDropDownList(ddlHotels, tableProvince, "HotelsName", "ID"); 
            InitDropDownList(ddlRoom, null); 
            if (required)
            {
                ddlRoom.CssClass = "cityRequired";
            }
        }
    }
}